//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.neurosky.thinkgear;

public class TGBlinkDetector {
    private short a = 0;
    private short b = 0;
    private short[] c = new short[512];
    private short[] d = new short[512];
    private int e = 0;
    public short blinkStart = -1;
    private short f = -1;
    private short g = -1;
    private short h = -1;
    private short i = -1;
    public short blinkEnd = -1;
    private short j = 0;
    private short k = 0;
    private short l = 0;
    private double m = 0.0D;

    public TGBlinkDetector() {
    }

    public byte Detect(int var1, short var2) {
        if(var1 < 51) {
            System.arraycopy(this.c, 1, this.d, 0, 511);
            this.d[511] = var2;
            System.arraycopy(this.d, 0, this.c, 0, 512);
            if(this.b < 512) {
                ++this.b;
            }

            if(this.b > 511) {
                short var10000;
                switch(this.e) {
                    case 0:
                        if(var2 > 230) {
                            this.blinkStart = -1;
                            this.g = -1;
                            this.h = -1;
                            this.i = -1;
                            this.blinkEnd = -1;
                            this.f = 511;
                            this.j = var2;
                            this.e = 1;
                        }

                        if(var2 < -200) {
                            this.blinkStart = -1;
                            this.g = -1;
                            this.h = -1;
                            this.i = -1;
                            this.blinkEnd = -1;
                            this.f = 511;
                            this.k = var2;
                            this.e = 4;
                        }
                        break;
                    case 1:
                        if(511 - this.f > 120 || this.f <= 0) {
                            this.e = 0;
                        }

                        --this.f;
                        if(var2 <= 230 && this.c[510] > 230) {
                            this.g = 510;
                        } else {
                            --this.g;
                        }

                        if(var2 > this.j) {
                            this.j = var2;
                        }

                        if(var2 < -200) {
                            this.h = 511;
                            this.k = var2;
                            if(this.h - this.g < 45) {
                                this.e = 2;
                            } else {
                                this.e = 0;
                            }
                        }
                        break;
                    case 2:
                        --this.f;
                        --this.g;
                        --this.h;
                        if(var2 >= -200 && this.c[510] < -200) {
                            this.i = 510;
                            this.e = 3;
                        } else {
                            --this.i;
                        }

                        if(var2 < this.k) {
                            this.k = var2;
                        }

                        if(511 - this.f > 120) {
                            this.i = 511;
                            this.e = 3;
                        }
                        break;
                    case 3:
                        --this.f;
                        --this.g;
                        --this.h;
                        if(var2 < -200) {
                            this.e = 2;
                        } else {
                            --this.i;
                        }

                        if(511 - this.i > 25 || var2 > 33) {
                            this.blinkEnd = 511;
                        }

                        if(this.blinkEnd > 0) {
                            for(this.a = 0; this.a < 25; ++this.a) {
                                this.blinkStart = (short)(this.f - this.a);
                                if(this.c[this.f - this.a] < 33) {
                                    break;
                                }
                            }

                            this.l = (short)(this.j - this.k);
                            if(this.l < 500) {
                                this.e = 0;
                                return (byte)0;
                            }

                            var10000 = this.l;
                            this.m = 0.0D;

                            for(this.a = this.blinkStart; this.a < this.blinkEnd + 1; ++this.a) {
                                this.m += (double)this.c[this.a];
                            }

                            this.m /= (double)(this.blinkEnd - this.blinkStart + 1);
                            if(this.m < 0.0D) {
                                this.m = -this.m;
                            }

                            if(this.m > 200.0D) {
                                this.e = 0;
                                return (byte)0;
                            }

                            if(this.blinkEnd - this.blinkStart < 50) {
                                this.e = 0;
                                return (byte)0;
                            }

                            if(this.c[this.blinkStart] <= 230 && this.c[this.blinkStart] >= -200) {
                                if(this.c[this.blinkEnd] <= 230 && this.c[this.blinkEnd] >= -200) {
                                    this.e = 0;
                                    return (byte)(this.l >> 4);
                                }

                                this.e = 0;
                                return (byte)0;
                            }

                            this.e = 0;
                            return (byte)0;
                        }
                        break;
                    case 4:
                        if(511 - this.f <= 120 && this.f > 0) {
                            --this.f;
                            if(var2 >= -200 && this.c[510] < -200) {
                                this.g = 510;
                            } else {
                                --this.g;
                            }

                            if(var2 < this.k) {
                                this.k = var2;
                            }

                            if(var2 > 230) {
                                this.h = 511;
                                this.j = var2;
                                if(this.h - this.g < 45) {
                                    this.e = 5;
                                } else {
                                    this.e = 0;
                                }
                            }
                            break;
                        }

                        this.e = 0;
                        return (byte)0;
                    case 5:
                        --this.f;
                        --this.g;
                        --this.h;
                        if(var2 <= 230 && this.c[510] > 230) {
                            this.i = 510;
                            this.e = 6;
                        } else {
                            --this.i;
                        }

                        if(var2 > this.j) {
                            this.j = var2;
                        }

                        if(511 - this.f > 120) {
                            this.i = 511;
                            this.e = 6;
                        }
                        break;
                    case 6:
                        --this.f;
                        --this.g;
                        --this.h;
                        if(var2 > 230) {
                            this.e = 5;
                        } else {
                            --this.i;
                        }

                        if(511 - this.i > 25 || var2 < 33) {
                            this.blinkEnd = 511;
                        }

                        if(this.blinkEnd > 0) {
                            for(this.a = 0; this.a < 25; ++this.a) {
                                this.blinkStart = (short)(this.f - this.a);
                                if(this.c[this.f - this.a] > 33) {
                                    break;
                                }
                            }

                            this.l = (short)(this.j - this.k);
                            if(this.l < 500) {
                                this.e = 0;
                                return (byte)0;
                            }

                            var10000 = this.l;
                            this.m = 0.0D;

                            for(this.a = this.blinkStart; this.a < this.blinkEnd + 1; ++this.a) {
                                this.m += (double)this.c[this.a];
                            }

                            this.m /= (double)(this.blinkEnd - this.blinkStart + 1);
                            if(this.m < 0.0D) {
                                this.m = -this.m;
                            }

                            if(this.m > 200.0D) {
                                this.e = 0;
                                return (byte)0;
                            }

                            if(this.blinkEnd - this.blinkStart < 50) {
                                this.e = 0;
                                return (byte)0;
                            }

                            if(this.c[this.blinkStart] <= 230 && this.c[this.blinkStart] >= -200) {
                                if(this.c[this.blinkEnd] <= 230 && this.c[this.blinkEnd] >= -200) {
                                    this.e = 0;
                                    return (byte)(this.l >> 4);
                                }

                                this.e = 0;
                                return (byte)0;
                            }

                            this.e = 0;
                            return (byte)0;
                        }
                        break;
                    default:
                        this.e = 0;
                }
            }
        } else {
            this.b = 0;
            this.f = -1;
            this.g = -1;
            this.h = -1;
            this.i = -1;
            this.e = 0;
        }

        return (byte)0;
    }
}
